package com.blockmeta.bbs.businesslibrary.flutter;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.businesslibrary.util.GateWayUtil;
import com.blockmeta.bbs.businesslibrary.util.JWTRefreshUtil;
import com.hellobike.flutter.thrio.navigator.PageRoute;
import com.hellobike.flutter.thrio.navigator.PageRoutes;
import com.hellobike.flutter.thrio.navigator.ThrioNavigator;

import java.io.File;
import java.util.List;

import androidx.core.content.FileProvider;
import io.flutter.embedding.android.ThrioFlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;

import static com.blockmeta.bbs.businesslibrary.launch.interestsYMAL.INTEREST_CONFIG;

public class MethodChannelFlutterPlugins {
    private static final String CHANNEL_NAME = "method_channel";
    public static boolean isReady = false;
    private static BinaryMessenger binaryMessenger;


    static MethodChannel methodChannel;

    public static void register(BinaryMessenger messenger) {

        methodChannel = new MethodChannel(messenger, CHANNEL_NAME);
        methodChannel.setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case "ready":
                    isReady = true;
                    result.success("");
                    break;
                case "getInterests":
                    String str = SharedPreferenceUtil.getString(BaseApp.getApp(), INTEREST_CONFIG, "");
                    Log.d("flutterdebug", "getInterests" + str);
                    result.success(str);
                    break;
                case "getAuthorization":
                    Log.d("flutterting", "getAuthorization");
                    result.success(GateWayUtil.getGateAuth());
                    break;
                case "refreshJWT":
                    Log.d("flutterting", "refreshJWT");
                    JWTRefreshUtil.refreshJWT();
                    result.success("");
                    break;
                case "install":
                    Log.d("flutterting", "install");
                    String path = call.argument("path");
                    Log.d("flutterting", "install" + path);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    //判断是否是AndroidN以及更高的版本
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        Uri contentUri = FileProvider.getUriForFile(BaseApp.getApp(), BaseApp.getApplicationId() + ".fileprovider", new File(path));
                        intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                    } else {
                        intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    BaseApp.getApp().startActivity(intent);
                    break;
                case "jumpAppStore":
                    Log.d("flutterting", "jumpAppStore");
                    break;
                default:
                    result.notImplemented();
                    break;
            }
        });

    }

    public static void send(String method, Object argument) {

        Log.d("flutterting", "send to flutter" + method);
        BaseApp.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (methodChannel == null) {
                    FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get("main");
                    if (flutterEngine != null) {
                        register(flutterEngine.getDartExecutor());
                    }
                }
                methodChannel.invokeMethod(method, argument);
            }
        });
    }

}
